<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage ProviderProduct
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The data model for provider product
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage ProviderProduct
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_ProviderProduct extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'provider_product';

    /**
     * Load data from file
     * @param string $filePath 
     */
    public function loadAllFromFile($filePath)
    {
        $result = 0;
        $sql = 'TRUNCATE ' . $this->_name;
        $this->getAdapter()->query($sql);
        $handle = @fopen($filePath, "r");
        if ($handle) {
            $batchSize = 1000;
            $sql = '';
            // Read header line
            $buffer = fgets($handle, 4096);
            // Read other lines
            while (($buffer = fgets($handle, 4096)) !== false) {
                $row = explode("\t", $buffer);
                $sql .= '(' . $this->getAdapter()->quote($row[1]) . ','
                        . $this->getAdapter()->quote($row[2]) . ','
                        . $this->getAdapter()->quote($row[3]) . ','
                        . $this->getAdapter()->quote($row[4]) . ','
                        . $this->getAdapter()->quote($row[5]) . ','
                        . $this->getAdapter()->quote($row[6]) . ','
                        . $this->getAdapter()->quote($row[11]) . ')';
                $result++;
                // Check accumulated values
                if ($result % $batchSize == 0 || feof($handle)) {
                    $sql = 'INSERT INTO ' . $this->_name
                            . ' (product_id,updated,quality,supplier_id,prod_id,catid,model_name) VALUES '
                            . $sql;
                    $this->getAdapter()->query($sql);
                    $sql = '';
                } else {
                    $sql .= ',';
                }
            }
            if (!feof($handle)) {
                echo "Error: unexpected fgets() fail\n";
            }
            fclose($handle);
            return $result;
        }
    }

    /**
     * Get list of products for import
     * @return array 
     */
    public function getAllForImport()
    {
        $query = $this->getAdapter()->select();
        $query->from(array('pp' => $this->_name), 'product_id');
        $query->join(array('p' => 'product'), 'p.mpn = pp.prod_id', '');
        $query->join(array('m' => 'manufacturer'), 'm.content_id = pp.supplier_id AND m.id = p.manufacturer_id', '');
        $query->where('p.imported IS NULL OR p.imported < pp.updated');
        $result = $this->getAdapter()->fetchCol($query);
        return $result;
    }

    public function getBySupplierIdAndName($supplierId, $name)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('product_id', 'prod_id', 'model_name', 'catid', 'supplier_id'));
        $query->where('supplier_id = ?', $supplierId);
        $query->where('model_name = ?', $name);
        $query->where("quality = 'ICECAT' OR quality = 'SUPPLIER'");
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

}
